# -*- coding:utf-8 -*-
import requests
# 下载图片
from PIL import Image

from utils.utils import huidu_JZ_keep_color, huidu_JZ, print_image


def dict_sort(dict):
    """
    字典排序（此处采用冒泡排序，耗时较长，待优化）
    :param dict:
    :return:
    """
    flag = None
    cache = []
    for item in dict.items():
        cache.append(item)

    for i in range(len(cache)):
        for j in range(len(cache) - i - 1):
            if cache[j][1] < cache[j + 1][1]:
                flag = cache[j + 1]
                cache[j + 1] = cache[j]
                cache[j] = flag
    return cache


response = requests.get("https://weixin.sogou.com/antispider/util/seccode.php")
with open("auth_code.jpg", "wb") as image_file:
    image_file.write(response.content)
# 打开图片
image = Image.open("auth_code.jpg")

width = image.width
height = image.height

image = huidu_JZ_keep_color(image, 105)
# image = huidu_JZ(image)

# print_image(image)

# # 按照颜色分布进行排序
# mapper_dict = {}
# for x in range(width):
#     for y in range(height):
#         pixel = image.getpixel((x, y))
#         if pixel in mapper_dict.keys():
#             mapper_dict[pixel] = mapper_dict[pixel] + 1
#             pass
#         else:
#             mapper_dict[pixel] = 1
#
# results = dict_sort(mapper_dict)
# # 截取其中最多的3个颜色
# results = results[1:100]
# result3_three = []
# for i in range(len(results)):
#     result3_three.append(results[i][0])
#
# for x in range(width):
#     for y in range(height):
#         pixel = image.getpixel((x, y))
#         if pixel in result3_three:
#             image.putpixel((x, y), (255, 255, 255))
image.save("result.jpg")

print()
